package cn.lpq.one_qrcode_pay.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.annotation.Configuration;

import com.egzosn.pay.common.http.HttpConfigStorage;
import com.egzosn.pay.spring.boot.core.PayServiceConfigurer;
import com.egzosn.pay.spring.boot.core.configurers.MerchantDetailsServiceConfigurer;
import com.egzosn.pay.spring.boot.core.configurers.PayMessageConfigurer;
import com.egzosn.pay.spring.boot.core.merchant.PaymentPlatform;
import com.egzosn.pay.spring.boot.core.provider.merchant.platform.AliPaymentPlatform;
import com.egzosn.pay.spring.boot.core.provider.merchant.platform.PaymentPlatforms;
import com.egzosn.pay.spring.boot.core.provider.merchant.platform.WxPaymentPlatform;

import cn.lpq.one_qrcode_pay.handlers.AliPayMessageHandler;
import cn.lpq.one_qrcode_pay.handlers.WxPayMessageHandler;
import cn.lpq.one_qrcode_pay.interceptor.AliPayMessageInterceptor;
import cn.lpq.one_qrcode_pay.pojo.AliPayProperty;
import cn.lpq.one_qrcode_pay.pojo.WxPayProperty;

/**
 * 支付服务配置
 *
 * @author egan
 *         email egzosn@gmail.com
 *         date 2019/5/26.19:25
 */
@Configuration
public class MerchantPayServiceConfigurer implements PayServiceConfigurer {

//    @Autowired
//    private JdbcTemplate jdbcTemplate;
    @Autowired
    private AutowireCapableBeanFactory spring;
    @Autowired
    private AliPayMessageHandler aliPayMessageHandler;
    @Autowired
    private WxPayMessageHandler wxPayMessageHandler;
    
	

	@Autowired
	private WxPayProperty wxPayProperty;
	@Autowired
	private AliPayProperty aliPayProperty;
	
    
    /**
     * 商户配置
     *
     * @param merchants 商户配置
     */
    @Override
    public void configure(MerchantDetailsServiceConfigurer merchants)  {
//        数据库文件存放 /doc/sql目录下
//        merchants.jdbc().template(jdbcTemplate);
        //微信请求配置，详情参考https://gitee.com/egzosn/pay-java-parent项目中的使用
        HttpConfigStorage wxHttpConfigStorage = new HttpConfigStorage();
//        wxHttpConfigStorage.setKeystore("http://www.egzosn.com/certs/ssl 退款证书");
//        wxHttpConfigStorage.setCertStoreType(CertStoreType.URL);
//        wxHttpConfigStorage.setStorePassword("ssl 证书对应的密码， 默认为商户号");
        //内存Builder方式
        merchants.inMemory()
                .ali()
                .detailsId("1")
                .appid("appid")
                //应用私钥
                .keyPrivate(aliPayProperty.getKeyPrivate())
                //支付宝公钥
                .keyPublic(aliPayProperty.getKeyPublic())
                .inputCharset("utf-8")
                .notifyUrl(aliPayProperty.getDomain()+"/PayDemo/payBack1.json")
                //.returnUrl("http://pay.egzosn.com/PayDemo/payBack1.json")
                .pid(aliPayProperty.getPid())
                .seller(aliPayProperty.getSeller())
                .signType("RSA2")
                .test(true)
                .and()
                .wx()
                .detailsId("2")
                .appid(wxPayProperty.getAppid())
                .mchId(wxPayProperty.getMchId())
                .secretKey(wxPayProperty.getSecretKey())
                .notifyUrl(wxPayProperty.getDomain()+"/PayDemo/payBack2.json")
                //.returnUrl("http://pay.egzosn.com/PayDemo/payBack2.json")
                .inputCharset("utf-8")
                .signType("MD5")
                //设置请求相关的配置
                .httpConfigStorage(wxHttpConfigStorage)
                .test(false)
                .and()
                .wx()
                .detailsId("3")
                .appid(wxPayProperty.getAppid())
                .mchId("1603931736")
                .secretKey("j8kPoAA3lBriQks9x0UK87IFqidPMMGi")
                .notifyUrl(wxPayProperty.getDomain()+"/PayDemo/payBack3.json")
                //.returnUrl("http://pay.egzosn.com/PayDemo/payBack2.json")
                .inputCharset("utf-8")
                .signType("MD5")
                //设置请求相关的配置
                .httpConfigStorage(wxHttpConfigStorage)
                .test(false)
                .and()
        ;

      /*  //------------内存手动方式------------------
        UnionMerchantDetails unionMerchantDetails = new UnionMerchantDetails();
        unionMerchantDetails.detailsId("3");
        //内存方式的时候这个必须设置
        unionMerchantDetails.setCertSign(true);
        unionMerchantDetails.setMerId("700000000000001");
        //公钥，验签证书链格式： 中级证书路径;
        unionMerchantDetails.setAcpMiddleCert("D:/certs/acp_test_middle.cer");
        //公钥，根证书路径
        unionMerchantDetails.setAcpRootCert("D:/certs/acp_test_root.cer");
        //私钥, 私钥证书格式： 私钥证书路径
        unionMerchantDetails.setKeyPrivateCert("D:/certs/acp_test_sign.pfx");
        //私钥证书对应的密码
        unionMerchantDetails.setKeyPrivateCertPwd("000000");
        //证书的存储方式
        unionMerchantDetails.setCertStoreType(CertStoreType.PATH);
        unionMerchantDetails.setNotifyUrl("http://127.0.0.1/payBack4.json");
        // 无需同步回调可不填  app填这个就可以
        unionMerchantDetails.setReturnUrl("http://127.0.0.1/payBack4.json");
        unionMerchantDetails.setInputCharset("UTF-8");
        unionMerchantDetails.setSignType("RSA2");
        unionMerchantDetails.setTest(true);
        //手动加入商户容器中
        merchants.inMemory().addMerchantDetails(unionMerchantDetails);*/
    }
    /**
     * 商户配置
     *
     * @param configurer 支付消息配置
     */
    @Override
    public void configure(PayMessageConfigurer configurer) {
        PaymentPlatform aliPaymentPlatform = PaymentPlatforms.getPaymentPlatform(AliPaymentPlatform.platformName);
        configurer.addHandler(aliPaymentPlatform, aliPayMessageHandler);
        configurer.addInterceptor(aliPaymentPlatform, spring.getBean(AliPayMessageInterceptor.class));
        configurer.addHandler(PaymentPlatforms.getPaymentPlatform(WxPaymentPlatform.platformName),wxPayMessageHandler);
    }
}
